<template>
  <div id="app">
    <router-view />
  </div>
</template>

<script>
import { mapGetters } from 'vuex'
import DevicePixelRatio from './utils/devicePixelRatio'
import { getSystemVersion } from '@/api/common/common'

import _ from 'lodash'
export default {
  name: 'App',
  computed: {
    ...mapGetters([
      'layerid', 'companyId'
    ])
  },
  data() {
    return {
      timer: undefined
    }
  },
  // created() {
  //   new DevicePixelRatio().init()
  // },
  mounted() {
    document.onkeydown = (e) => {
      const keyCode = window.event.keyCode
      switch (keyCode) {
        case 27:
          if (this.layerid.size) {
            const last = Array.from(this.layerid)[this.layerid.size - 1]
            this.$layer.close(last) // 关闭弹窗
            this.$store.commit('app/SET_LAYERID', last)
          }
          break
        // case 13: // 阻止回车事件
        //   return false
      }
    }

    // const setViewport = function() {
    //   // 计算当前屏幕的宽度与设计稿比例
    //   const scale = window.screen.width / DESIGN_WIDTH
    //   // 获取元素
    //   let meta = document.querySelector('meta[name=viewport]')
    //   const content = `width=${DESIGN_WIDTH}, initial-scale=${scale}, maximum-scale=${scale}, minimum-scale=${scale}`
    //   // 判断是否已存在
    //   if (!meta) {
    //     meta = document.createElement('meta')
    //     meta.setAttribute('name', 'viewport')
    //     document.head.appendChild(meta)
    //   }
    //   meta.setAttribute('content', content)
    // }
    window.addEventListener('resize', () => { // 窗口大小改变时触发事件
      // _.debounce(function(event) { // 加防抖方式二
      //   this.$store.state.app.windowResizeTag = Date.now() // 修改vuex里的窗口改变标识，让其它页面也能监听到页面大小变化
      // }, 500)

      this.$Debounce(() => { // 加防抖方式一
        this.$store.state.app.windowResizeTag = Date.now() // 修改vuex里的窗口改变标识，让其它页面也能监听到页面大小变化
      }, 500)()
    })

    if (process.env.VUE_APP_BACKEND_SYNC == '1') {
      this.timer = setInterval(() => {
        this.getSystemVersion()
      }, 60 * 1000)
    }
  },
  beforeDestroy() {
    // 页面关闭清除定时器
    clearInterval(this.timer)
  },
  methods: {
    getSystemVersion() {
      getSystemVersion().then(res => {
        console.log('oldVersion:' + this.$store.state.app.appVersion)
        console.log('newVersion:' + res.data)
        if (!this.$store.state.app.appVersion) {
          this.$store.state.app.appVersion = res.data
        } else if (this.$store.state.app.appVersion != res.data) {
          // 刷新页面
          location.reload()
        }
      })
    }
  }
}
</script>
<style>
@import "assets/font/font.css";
#app{
  width: 100%;
  height: 100%;
}
html{
  width: 100%;
  height: 100%;
}
body{
  width: 100%;
  height: 100%;
  margin: 0px;
}
</style>
